// Resources.cs created with MonoDevelop
// User: kpr at 17:04 19.10.2007
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
// created on 19.10.2007 at 17:04


using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace Designer
{
	public static class Helper
	{
		public static bool IsNumeric(string text)
		{
			return System.Text.RegularExpressions.Regex.IsMatch(text,"^-?\\d*$");
		}
	}
	
	public enum EventType
	{
		Create, Step, Draw, Null
	}
	
	public class Resource
	{
		protected string name;
		
		virtual public string Name
		{
		get
		{
			return name;
		}
		}
		public void SetName(string name)
		{
			this.name = name;
		}
		public Hashtable Settings;
		virtual public string GetResType()
		{
			return "Undefined";
		}
		public Hashtable GetSettings()
		{
			return Settings;
		}
		public void SetSettings(XmlNode node, List<ArrayList> rlist)
		{
			string key = node.Attributes["name"].InnerText;
			if(node.ChildNodes.Count < 1)
			{
				Settings[key] = node.InnerText;
			}
			else
			{
				XmlNode child = node.ChildNodes[0];
				object Value = ResourceMgr.Instance.ReadXmlValue(child, true);
				if(Value is ArrayList)
				{
					ArrayList arrlist = (ArrayList) Value;
					arrlist.Add(this);
					arrlist.Add(key);
					rlist.Add(arrlist);
				}
				else
				{
					if(Settings.ContainsKey(key) && Settings[key] is FileResource)
						FileResourceMgr.Instance.Resources.Remove(Settings[key] as FileResource);
						
					Settings[key] = Value;
				}
				
			}
		}
	}
	
	public class Event : Resource,ICloneable
	{
		public EventType type;
		public List<Action> actions = new List<Action>();
		public Obj Parent;
		
		public Event(EventType type, Obj parent)
		{
			this.type = type;
			this.Parent = parent;
		}
		override public string GetResType()
		{
			return "Event";
		}
		override public string Name
		{
			get
			{
			if(type == EventType.Create)
				return "Create";
			if(type == EventType.Step)
				return "Step";
			if(type == EventType.Draw)
				return "Draw";
			return "Undefined";
			}
		}
		
		public object Clone()
		{
			Event x = new Event(this.type,this.Parent);
			foreach(Action a in actions)
			{
				x.actions.Add((Action)a.Clone());
			}
			return x;
		}
		
		public static EventType StringType(string n)
		{
			switch(n)
			{
				case "Create":
					return EventType.Create;
				case "Step":
					return EventType.Step;
				case "Draw":
					return EventType.Draw;
				default:
					return EventType.Null;
			}
		}
	}
	
	public class Obj:Resource, ICloneable
	{
		override public string GetResType()
		{
			return "Object";
		}
		public Obj(string name)
		{
			this.name = name;
			Settings = new Hashtable();
			Settings["visible"] = true;
			Settings["persistent"] = false;
			Settings["depth"] = 0;
		}
		public List<Event> events = new List<Event>();
		public object Clone()
		{
			Obj o = new Obj(this.name);
			o.Settings = (Hashtable)this.Settings.Clone();
			foreach(Event e in events)
			{
				o.events.Add((Event)e.Clone());
			}
			return o;
		}
	}
	public class Font:Resource
	{
		override public string GetResType()
		{
			return "Font";
		}
		public Font(string name)
		{
			this.name = name;
			Settings = new Hashtable();
			Settings["file"]=new FileResource();
			Settings["size"]=12;
			//Settings["style"]=0; // 1 for bold, 2 for italic, 4 for underlined
		}
	}
	public class Room:Resource
	{
		override public string GetResType()
		{
			return "Room";
		}
		public Room(string name)
		{
			this.name = name;
			Settings = new Hashtable();
			Settings["width"] = 640;
			Settings["height"] = 480;
			Settings["persistent"] = false;
			Settings["speed"] = 30;
			Settings["caption"] = "";
			Settings["gridx"] = 16;
			Settings["gridy"] = 16;
			Settings["bgcolor"] = new Gdk.Color(0,0,0);
			Settings["lastobject"] = null;
		}
		public List<Inst> instances = new List<Inst>();
		public void DeleteInstancesOfObj(Obj o)
		{
			for(int i = 0; i < instances.Count; i++)
			{
				if(instances[i].obj == o)
				instances.RemoveAt(i--);
			}
		}
	}
	public class Cla:Resource
	{
		override public string GetResType()
		{
			return "Class";
		}
		public Cla(string name)
		{
			this.name = name;
			Settings = new Hashtable();
		}
	}

	public class Inst
	{
		public Inst(Obj obj, int x, int y)
		{
			this.obj = obj;
			this.x = x;
			this.y = y;
		}
		public Inst(Inst inst)
		{
			this.obj = inst.obj;
			this.x = inst.x;
			this.y = inst.y;
		}
		public Obj obj;
		public int x;
		public int y;
		public bool CheckHover(int x, int y)
		{
			//TODO: Add sprite hover checking
			return (x >= this.x && x <= this.x+10 && y >= this.y && y <= this.y+10);
		}
	}
	public class Gra:Resource
	{
		override public string GetResType()
		{
			return "Sprite";
		}
		public Gra(string name)
		{
			this.name = name;
			Settings = new Hashtable();
			file = "";
		}
		public string file;
	}
	public class Back:Resource
	{
		override public string GetResType()
		{
			return "Background";
		}
		public string file;
		public Back(string name)
		{
			this.name = name;
			Settings = new Hashtable();
			file = "";
		}
	}
	
	public class Action : ICloneable
	{
		public string Title;
		public string Type;
		public ActionSet Set;
		public string Location;
		public ArrayList Args = new ArrayList();
		public Action(string Type, string Location, ActionSet Set)
		{
			this.Set = Set;
			this.Type = Type;
			this.Title = Type;
			this.Location = Location;
		}
		public void OpenEditWin()
		{
			Set.OpenActionEditWin(this, ResourceMgr.Instance.Win);
		}
		public static string GetLocation(Designer.Obj obj, Event ev)
		{
			return obj.Name +" - "+ ev.Name;
		}
		public object Clone()
		{
			return this.MemberwiseClone();
		}
	}
	public class Code:Resource , ICloneable
	{
		override public string GetResType()
		{
			return "Code";
		}
		public string language;
		public string code;
		public string title;
		public Code(string code, string title)
		{
			this.name = "Code";
			this.code = code;
			this.title = title;
			this.language = "GML";
		}

		public object Clone()
		{
			return this.MemberwiseClone();
		}	
	}
	
}
